package edu.xidian.bdilab.jrbigdata.service.admin.adminlogin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.xidian.bdilab.jrbigdata.controller.admin.AdminCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.AdminUser;
import edu.xidian.bdilab.jrbigdata.mapper.AdminUserMapper;
import edu.xidian.bdilab.jrbigdata.service.admin.adminlogin.AdminLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class AdminLoginServiceImpl implements AdminLoginService {

    @Autowired
    AdminUserMapper adminUserMapper;

    @Override
    public ResponseResult adminLogin(String userName, String password) throws Exception{
        QueryWrapper<AdminUser> adminUserQueryWrapper=new QueryWrapper<>();
        adminUserQueryWrapper.eq("user_name",userName);
        ResponseResult responseResult=new ResponseResult();
        try{
            AdminUser adminUser=adminUserMapper.selectOne(adminUserQueryWrapper);
            if (adminUser==null){
                responseResult.setCode(AdminCode.NOT_EXIST.getCode());
                responseResult.setMessage(AdminCode.NOT_EXIST.getMessage());
                responseResult.setSuccess(false);
                return responseResult;
            }else{
                if(adminUser.getPassword().equals(password)){
                    responseResult.setCode(AdminCode.Login_SUCCESS.getCode());
                    responseResult.setMessage(AdminCode.Login_SUCCESS.getMessage());
                    responseResult.setSuccess(true);
                    return responseResult;
                }
            }
            responseResult.setCode(AdminCode.Login_FAIL.getCode());
            responseResult.setMessage(AdminCode.Login_FAIL.getMessage());
            responseResult.setSuccess(false);
            return responseResult;

        }catch (Exception e){
            responseResult.setCode(AdminCode.Login_FAIL.getCode());
            responseResult.setMessage(AdminCode.Login_FAIL.getMessage());
            responseResult.setSuccess(false);
            return responseResult;
        }
    }
}
